package com.labraff.todo;

import androidx.appcompat.app.AppCompatActivity;

import com.prolificinteractive.materialcalendarview.MaterialCalendarView;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private globalApplication app;
    private TextView tv_undoNumber;
    private TextView tv_doneNumber;
    private TextView tv_noTask;
    private Button btn_addTask;
    private RelativeLayout undoCard,doneCard;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        app = ((globalApplication) getApplication());

        // 视图绑定初始化
        tv_undoNumber = findViewById(R.id.undoNumber);
        tv_doneNumber = findViewById(R.id.doneNumber);
        tv_noTask = findViewById(R.id.noTask);
        btn_addTask = findViewById(R.id.addTask);
        undoCard = findViewById(R.id.undoCard);
        doneCard = findViewById(R.id.doneCard);

        // 基本初始化
        init();

        // 更新视图数据
        viewUpdate();

        taskList_adapter adapter = new taskList_adapter(this, app.getUrgentTaskList(),2);
        ListView todoListView = findViewById(R.id.todoListView);
        todoListView.setAdapter(adapter);
        todoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                task currentTask = app.getTaskList().get(position);
                Toast.makeText(MainActivity.this, currentTask.getTitle(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void viewUpdate() {
        int doneTask = app.getDoneTaskList().size();
        int undoTask = app.getUndoTaskList().size();
        int tasks = app.getUrgentTaskList().size();
        if (tasks == 0) {
            tv_noTask.setVisibility(View.VISIBLE);
        } else {
            tv_noTask.setVisibility(View.GONE);
        }
        tv_doneNumber.setText(String.valueOf(doneTask));
        tv_undoNumber.setText(String.valueOf(undoTask));
    }

    public void init() {
        btn_addTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, CreateTask.class);
                startActivity(intent);
            }
        });

        undoCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, TaskListView.class);
                intent.putExtra("taskType",0);
                startActivity(intent);
            }
        });

        doneCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, TaskListView.class);
                intent.putExtra("taskType",1);
                startActivity(intent);
            }
        });
    }


}



